package com.poi.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.io.File;
import java.util.Map;

@SuppressWarnings("serial")
public class importexcel extends ActionSupport {
	//public static boolean flag = false;
	//public static String prompt = "成功导入";
	HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
	private final static String PATH = "upload";
	public static boolean flag = false;
	public  static String prompt="";
	public static String massage="";
	public static String uploadsql;
	private String ctable;	//xml文件
	private File upload;//上传的excel表
	private String uploadContentType;//excel表的文件后缀名
	private String uploadFileName;//excel表的文件名
//	private String uploadsql;//需要导入的数据库中的表
	@SuppressWarnings("rawtypes")
	Map attribute = ActionContext.getContext().getSession();
	//private String result;

	@SuppressWarnings("unchecked")
	public String execute() throws Exception{
		prompt="";
		ctable=selecttable.ctable;
		if(ctable.equals("")||uploadFileName==null)
			return INPUT;
		System.out.println("文件名"+uploadFileName);
		System.out.println("文件后缀"+uploadContentType);
		System.out.println("导入表的xml规则："+ctable);				
		uploadsql = ctable;
		System.out.println("导入的表名："+uploadsql);
		String pathexcel = upload.toString();
		System.out.println("pathexcel="+pathexcel);
		ctable=ServletActionContext.getServletContext().getRealPath("/xmlguize")+"/"+ctable+".xml";
		System.out.println("catable="+ctable);
		File xmlFile = new File(ctable);		
	    new parseExcelUtil(upload,uploadFileName,xmlFile);
		if(flag==true)
		{
			
			JOptionPane.showMessageDialog(null,
		    "成功导入",
		            "信息",JOptionPane.PLAIN_MESSAGE);
			prompt="成功导入"+selecttable.tablename;
			request.getSession().setAttribute("prompt", prompt);
			return INPUT;
		}
		else
		{	
			request.getSession().setAttribute("prompt", "文件中存在错误信息,不能导入,错误信息如下：<br />"+prompt);
			return INPUT;
		}
		
	}
	
	
	
	public String getCtable() {
		return ctable;
	}
	public void setCtable(String ctable) {
		this.ctable = ctable;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
}
